/* 
 * GWT-Ext Widget Library
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated 
 * by the @authors tag. See the copyright.txt in the distribution for a 
 * full listing of individual contributors. 
 * 
 * This is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * This software is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this software; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 */

package edu.rpi.tw.pml.browsing.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.ExtElement;
import com.gwtext.client.data.Tree;
import com.gwtext.client.core.EventObject;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
import edu.rpi.tw.pml.browsing.client.util.JSONParser;

/**
 * A UI Widget that allows show a concept tree.
 */
public class ConceptTreeWidget {

	private final String LOADING_MSG = "Loading concepts";
	private final FacetSearchServiceAsync facetSearchService;
	private ArrayList<String> conceptArray = new ArrayList<String>();
	private final TreePanel treePanel = new TreePanel();

	private final TableDataWidget tdw;
	private final FacetSetWidget fsw;

	/*
	 * Constructor with an array of concepts.
	 */
	public ConceptTreeWidget(TableDataWidget itw, FacetSetWidget fw) {
		tdw = itw;
		fsw = fw;
		treePanel.setTitle("Concept Tree");
		treePanel.setId("mask-target");
		treePanel.setCollapsible(true);
		treePanel.setWidth(300);
		treePanel.setAnimate(true);
		treePanel.setContainerScroll(true);
		treePanel.setAutoScroll(true);
		treePanel.setRootVisible(true);
		treePanel.setFrame(true);

		final TreeNode root = new TreeNode("PML Concepts");
		treePanel.setRootNode(root);
		treePanel.expandAll();

		// Load data via RPC.
		facetSearchService = GWT.create(FacetSearchService.class);
		facetSearchService.getAllConcepts(new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out
						.println("Error while loading data to initialize concept tree.");
			}

			public void onSuccess(String result) {
				conceptArray = JSONParser.string2ConceptArray(result);

				for (int i = 0; i < conceptArray.size(); i++) {
					// Set full URI as tree node tooltips.
					String uri = conceptArray.get(i);
					String[] part = uri.split("#");
					TreeNode child = new TreeNode(part[1]);
					child.setChecked(false);
					child.setId(uri);
					child.setTooltip(uri);
					root.appendChild(child);
				}
			}
		});

		TreePanelListenerAdapter tpla = new TreePanelListenerAdapter() {
			public void onAppend(Tree tree, TreeNode parent, TreeNode node,
					int index) {
				final ExtElement element = Ext.get("mask-target");
				int size = conceptArray.size();
				if (index < size - 1) {
					// Masking panel while loading.
					element.mask(LOADING_MSG);
				} else {
					// Unmask when finish loading.
					element.unmask();
				}
			}
		};
		treePanel.addListener(tpla);

		Toolbar bottomToolbar = new Toolbar();
		bottomToolbar.addFill();
		bottomToolbar.addButton(new ToolbarButton("Show Selected",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						TreeNode[] checkedNodes = treePanel.getChecked();
						int size = checkedNodes.length;
						if (size != 0) {
							ArrayList<String> concepts = new ArrayList<String>();
							for (int i = 0; i < size; i++) {
								concepts.add(checkedNodes[i].getId());
							}
							// Update instance table and facet lists.
							tdw.setConcepts(concepts);
						} else {
							MessageBox.alert("Warning", "No concept selected.");
						}
					}
				}));

		treePanel.setBottomToolbar(bottomToolbar);
	}

	public TreePanel getPanel() {
		return this.treePanel;
	}
}